//
//  SendAzkarController.m
//  Azkar
//
//  Created by 王易 on 2018/8/28.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "SendAzkarController.h"
#import "CommentsView.h"
@interface SendAzkarController ()
@property (nonatomic,weak) UILabel *placeholder;
@property (nonatomic,weak) UITextView *inputView;
@property (nonatomic,assign) BOOL isAnonymously;
@end

/**
 发祈福页
 */
@implementation SendAzkarController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initNavigation];
    [self initUI];
}

- (void)initUI
{
    MyRelativeLayout *rootLayout = [MyRelativeLayout new];
    rootLayout.myBottom = [PhoneUtil tabbarAdjustHeight];
    rootLayout.myTop = [PhoneUtil topAdjustHeightWithNavi:YES];
    rootLayout.myHorzMargin= 0;
    rootLayout.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:rootLayout];
    

    
//    CommentsView *commentsView = [CommentsView new];
//    commentsView.lang_type = [BaseApi standard].language;
//    [commentsView initContentUI];
//    commentsView.sendBLock = ^(UITextField *input){
//
//    };
//    [rootLayout addSubview:commentsView];
    
    
    UIScrollView *contentScrollView = [UIScrollView new];
    contentScrollView.myTop = 0;
    contentScrollView.myHorzMargin = 0;
    contentScrollView.myBottom = [PhoneUtil tabbarAdjustHeight];
    [rootLayout addSubview:contentScrollView];
    
    MyRelativeLayout *contentLayout = [MyRelativeLayout new];
    contentLayout.myHorzMargin = 0;
    contentLayout.myTop = 0;
    contentLayout.wrapContentHeight = YES;
    [contentScrollView addSubview:contentLayout];
    
    [self addMoreLayout:contentLayout];
}
- (void)addMoreLayout:(MyBaseLayout *)superLayout
{
    UILabel *placeholder = [UILabel new];
    self.placeholder = placeholder;
    placeholder.myLeading = 16;
    placeholder.myTop = 26;
    placeholder.wrapContentSize = YES;
    placeholder.text = TLString(@"greeting_cards_what_do_you_want_to_pray_for");
    placeholder.font = FontSize(16);
    placeholder.textColor = Color(@"#929292");
    [superLayout addSubview:placeholder];
    
    UITextView *inputView = [UITextView new];
    self.inputView = inputView;
    inputView.backgroundColor = [UIColor clearColor];
    inputView.textColor = Color(@"#002812");
    inputView.font = FontSize(15);
    inputView.myLeading = inputView.myTrailing = 12;
    inputView.myTop = 20;
    inputView.myHeight = 180;
    [superLayout addSubview:inputView];
    WeakSelf
    [inputView.rac_textSignal subscribeNext:^(id x) {
        StrongSelf
        NSString *text = x;
        if (text.length>0) {
            self.placeholder.myVisibility = MyVisibility_Gone;
            self.myNavigationBar.rightButton1.enabled = YES;
           
             [self.myNavigationBar.rightButton1 changeTitleColor:Color(@"#00732F")];
        }else{
            self.placeholder.myVisibility =MyVisibility_Visible;
            self.myNavigationBar.rightButton1.enabled = NO;
            [self.myNavigationBar.rightButton1 changeTitleColor:Color(@"#D6D6D6")];
        }
        
    }];
    
    MyRelativeLayout *anonymouslyLayout = [MyRelativeLayout new];
    
    anonymouslyLayout.myLeading = 14;
    anonymouslyLayout.topPos.equalTo(inputView.bottomPos).offset(20);
    anonymouslyLayout.wrapContentSize = YES;
    [superLayout addSubview:anonymouslyLayout];
    
    __block UIImageView *imageV = [UIImageView new];
    imageV.myWidth = imageV.myHeight = 16;
    imageV.image = Image(@"selection_box");
    imageV.myLeading = imageV.myCenterY = 0;
    [anonymouslyLayout addSubview:imageV];
    
   __block UILabel *anonymously = [UILabel new];
    anonymously.text = TLString(@"greeting_cards_Anonymously");
    anonymously.textColor = Color(@"#929292");
    anonymously.font = FontSize(14);
    anonymously.leadingPos.equalTo(imageV.trailingPos).offset(8);
    anonymously.myTop = 0;
    anonymously.wrapContentSize = YES;
    [anonymouslyLayout addSubview:anonymously];
    
    [anonymouslyLayout hyb_addTapGestureWithCallback:^(UITapGestureRecognizer *sender) {
        StrongSelf
        self.isAnonymously = !self.isAnonymously;
        if (self.isAnonymously) {
            anonymously.textColor = Color(@"#525252");
            imageV.image = Image(@"selection_box_s");
        }else{
            anonymously.textColor = Color(@"#929292");
            imageV.image = Image(@"selection_box");
        }
    }];
}
- (void)initNavigation{
    
    [self.navigationController setNavigationBarHidden:YES];
    [self initLeftBackNaviButton];
    [self.myNavigationBar.backButton setTitle:TLString(@"greeting_cards_Release") forState:UIControlStateNormal];
    [self.myNavigationBar.backButton changeTitleColor:Color(@"#001208")];
    [self.myNavigationBar.rightButton1 setTitle:TLString(@"social_send") forState:UIControlStateNormal];
    [self.myNavigationBar.rightButton1 changeTitleColor:Color(@"#D6D6D6")];
    WeakSelf
    [self.myNavigationBar.rightButton1 setHyb_onTouchUp:^(UIButton *sender) {
        StrongSelf
        [self.inputView endEditing:YES];
        [self submitAzkar];
    }];

}
- (void)onLeftButtonTouched
{
    [self.inputView endEditing:YES];
    if (self.inputView.text.length>0) {
        
        MyAlertViewV3 *a = [MyAlertViewV3 alertViewWithTitle:TLString(@"greeting_cards_drop_hint") block:^(NSInteger btnIdx) {
            if (btnIdx == 1) {
                //1是确定
                [self.navigationController popViewControllerAnimated:YES];
            }
        }];
        [a addActionWithTitle:TLString(@"greeting_cards_cancel") color:UIColorHex(#334139)];
        [a addActionWithTitle:TLString(@"YES") color:UIColorHex(#00732F)];
        [a show:NO];
    }else{
//        RoutePop
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark - 提交评论
- (void)submitAzkar
{

    [AlertTools showLoading];
    [[AzkarApi postData:BlessBlog withParm:@{@"country": [LanguageManager defaultManager].appLanguage,@"content":self.inputView.text,@"lang_type":[BaseApi standard].language,@"is_anonymous":self.isAnonymously ? @"1": @"0"}]subscribeNext:^(id x) {
        [AlertTools hideLoading];
        [[NSNotificationCenter defaultCenter]postNotificationName:SendAzkarSuccess_Const object:nil];
        SuccessView *successV = [SuccessView new];
        successV.SuccessBlock = ^{
            [self.navigationController popViewControllerAnimated:YES];
        };
        [successV showView];
        
        
    } error:^(NSError *error) {
//        [AlertTools hideLoading];
    }];
}
 - (BOOL)gestureRecognizerShouldBegin
{
    return NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
